package com.be.beadmin.service.controller;

import com.be.beadmin.common.entity.PageEntity;
import com.be.beadmin.service.dto.input.NotifyInfoTotalByPage;
import com.be.beadmin.service.dto.output.NotifyTemplateInfoEx;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.data.domain.Page;

import com.be.beadmin.service.service.NotifyTemplateInfoService;
import com.be.beadmin.service.dto.input.NotifyTemplateInfoDto;
import com.be.beadmin.common.utils.ResultModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.io.IOException;
import java.util.List;
import java.util.Map;

import com.alibaba.excel.EasyExcel;

/**
* @author ljh
* @date 2023-09-20
**/
@Api(tags = {"站内信管理"})
@RestController
public class NotifyTemplateInfoController {

    @Autowired
    private  NotifyTemplateInfoService notifyTemplateInfoService;

    @ApiOperation("已读全部")
    @PostMapping(value = "/service/readAllNotify")
    public ResultModel readAllNotify(
            @ApiParam(value = "currentUserName") @RequestParam String userName,
            @ApiParam(value = "notifyType") @RequestParam String notifyType,
            @ApiParam(value = "status(负数)") @RequestParam int status
          )  {
        notifyTemplateInfoService.readAllNotify(userName,notifyType,status);
        return ResultModel.success(ResultModel.UPDATE);
    }

    @ApiOperation("已读，单个/批量/全部")
    @PostMapping(value = "/service/readOneOrManyOrAllNotify")
    public ResultModel readOneOrManyOrAllNotify(
            @ApiParam(value = "currentUserName") @RequestParam String userName,
            @ApiParam(value = "notifyType") @RequestParam String notifyType,
            @ApiParam(value = "status(负数)") @RequestParam int status,
            @ApiParam(value = "List<NotifyTemplateInfoId>") @RequestParam(required = false) List<String> notifyTemplateInfoIdList)  {
        notifyTemplateInfoService.readOneOrManyOrAllNotify(userName,notifyType,status,notifyTemplateInfoIdList);
        return ResultModel.success(ResultModel.UPDATE);
    }

    @ApiOperation("站内信自定义分页查询")
    @PostMapping(value = "/service/getNotifyInfoTotalByPage")
    public ResultModel getNotifyInfoTotalByPage(@RequestBody NotifyInfoTotalByPage notifyInfoTotalByPage)  {
        return ResultModel.success(notifyTemplateInfoService.getNotifyInfoTotalByPage2(notifyInfoTotalByPage)) ;
    }

    @ApiOperation("获取 指定用户 所有未读未处理的消息")
    @PostMapping(value = "/service/findNotReadNotifyTemplateInfoOfUserName")
    public ResultModel findNotReadNotifyTemplateInfoOfUserName(@RequestParam String userName)  {
        Map<String, List<Object>> maps = notifyTemplateInfoService.findNotReadNotifyTemplateInfoOfUserName(userName);
        return ResultModel.success(maps) ;
    }


    @PostMapping(value = "/service/getNotifyTemplateInfoByPage")
    public ResultModel getNotifyTemplateInfoByPage(@RequestBody PageEntity pageEntity)  {
        return ResultModel.success(notifyTemplateInfoService.getNotifyTemplateInfoByPage(pageEntity)) ;
    }

    @PostMapping("/service/addNotifyTemplateInfo")
    public ResultModel addNotifyTemplateInfo(@RequestBody NotifyTemplateInfoDto notifyTemplateInfoDto){
        notifyTemplateInfoService.addNotifyTemplateInfo(notifyTemplateInfoDto);
        return ResultModel.success(ResultModel.ADD);
    }

    @ApiOperation("删除一条")
    @PostMapping("/service/delNotifyTemplateInfo")
    public ResultModel delNotifyTemplateInfo(
            @ApiParam(value = "NotifyTemplateInfoId") @RequestParam(required = false) String notifyTemplateInfoId){
        notifyTemplateInfoService.delNotifyTemplateInfo(notifyTemplateInfoId);
        return ResultModel.success(ResultModel.DELETE);
    }

    @ApiOperation("清空全部已读、待办")
    @PostMapping("/service/delNotifyTemplateInfos")
    public ResultModel delNotifyTemplateInfos(
            @ApiParam(value = "userName") @RequestParam String userName,
            @ApiParam(value = "status") @RequestParam int status
    ){
        notifyTemplateInfoService.delNotifyTemplateInfos(userName,status);
        return ResultModel.success(ResultModel.DELETE);
    }


    @PostMapping("/service/updateNotifyTemplateInfo")
    public ResultModel updateNotifyTemplateInfo(@RequestBody NotifyTemplateInfoDto notifyTemplateInfoDto){
        notifyTemplateInfoService.updateNotifyTemplateInfo(notifyTemplateInfoDto);
        return ResultModel.success(ResultModel.UPDATE);
    }


    @PostMapping("/service/exportNotifyTemplateInfo")
    public void exportNotifyTemplateInfo(HttpServletResponse response ,@RequestBody PageEntity pageEntity) throws IOException {
        Page<NotifyTemplateInfoEx> notifyTemplateInfoPage = notifyTemplateInfoService.getNotifyTemplateInfoByPage(pageEntity);
        List<NotifyTemplateInfoEx> notifyTemplateInfoExes = notifyTemplateInfoPage.getContent();
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("NotifyTemplateInfo接口", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), NotifyTemplateInfoEx.class).sheet("sheet1").doWrite(notifyTemplateInfoExes);
    }
}